package input;

import instance.Instance;
import instance.InstanceTP3;


import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.StringTokenizer;


public class inputTP3 implements Input{

	private String inputFile;
	private LinkedList<Instance> instances;
	
	public inputTP3(String inputFile) {
		this.inputFile = inputFile;

	}
    //nos guardamos todas las instancias del archivo de entrada para procesarlos de a uno
	
	@Override
	public void readFile() throws Exception {
				
		BufferedReader bufferedReader = new BufferedReader(new FileReader(
				new File(inputFile)));

		instances = new LinkedList<Instance>();

		while (bufferedReader.ready()) {
			Instance instance =  readInstance(bufferedReader);
			instances.add( instance);
		}
		
		bufferedReader.close();	
	}

	@Override
	public LinkedList<Instance> getInstances() {
	
		return instances;
	}

	@Override
	public Instance readInstance(BufferedReader bufferedReader)
			throws Exception {

		InstanceTP3 subInstance ;
		String line = bufferedReader.readLine();
		StringTokenizer n_y_v = new StringTokenizer(line);
		Integer n = Integer.parseInt(n_y_v.nextToken().trim());
		Integer v = Integer.parseInt(n_y_v.nextToken().trim())-1;
		subInstance = new InstanceTP3(n, v);
		
		for(int i = 1; i<n;i++){
		/*Ejes desde el nodo "i"*/
			line = bufferedReader.readLine();
			StringTokenizer tokens = new StringTokenizer(line);
			int col = 0;
			while(tokens.hasMoreTokens()){
				Integer eje = Integer.parseInt(tokens.nextToken().trim());
				subInstance.matriz[i][col] = eje;
				subInstance.matriz[col][i] = eje;
				col++;
			}
		}
		for(int i=0; i<n; i++){
			subInstance.matriz[i][i] = -1;
		}
		return subInstance;		
	}
	/*public static void main(String[] args) throws Exception  {
		inputTP3 ip = new inputTP3("./src/resources/Tp3.in");
		ip.readFile();
		LinkedList<Instance> instances = ip.getInstances();
		Iterator<Instance> it = instances.iterator();
		while (it.hasNext()){
			InstanceTP3 inst = (InstanceTP3) it.next();
			int n = inst.getN();
			int matriz[][] = inst.matriz;
			n = n+1-1;
			matriz[0][0] = matriz[0][0] +1-1;
			
		}
		

		
	}*/
		

}
